package com.generic.genericinterface.demo2;

import java.util.Iterator;

/**
 * 通过适配器，使Fibonacci实现Iterable接口
 * 
 * @author Administrator
 * 
 */
public class IterableFibonacci extends Fibonacci implements Iterable<Integer> {

	private int n;

	public IterableFibonacci(int count) {
		n = count;
	}

	@Override
	public Iterator<Integer> iterator() {
		return new Iterator<Integer>() {

			@Override
			public boolean hasNext() {
				return n > 0;
			}

			@Override
			public Integer next() {
				IterableFibonacci.this.n--;
				return IterableFibonacci.this.next();
			}

			@Override
			public void remove() {
				// TODO Auto-generated method stub

			}
		};
	}

	public static void main(String[] args) {
		IterableFibonacci gen = new IterableFibonacci(18);
		for (Integer integer : gen) {
			System.out.println(integer);
		}
	}

}
